/**GameDemo
 * @date   2011-4-9
 * @author tianye
 */
package cn.edu.dlut.WingGameDemos;

import android.graphics.Rect;
import android.os.Bundle;
import cn.edu.dlut.WingGame.BaseType.ObjectState;
import cn.edu.dlut.WingGame.BaseType.WingColor;
import cn.edu.dlut.WingGame.GUI.WingBitmapButton;
import cn.edu.dlut.WingGame.GUI.WingGUI;
import cn.edu.dlut.WingGame.Game.WingActivity;
import cn.edu.dlut.WingGame.Game.WingGame;
import cn.edu.dlut.WingGame.Game.WingLayer;
import cn.edu.dlut.WingGame.Game.WingScene;
import cn.edu.dlut.WingGame.Graphic.GLDevice;
import cn.edu.dlut.WingGame.Graphic.GLTexture;
import cn.edu.dlut.WingGame.IOsystem.WingTouch;
import cn.edu.dlut.WingGame.Object.WingFont;
import cn.edu.dlut.WingGame.Object.WingImage;
import cn.edu.dlut.WingGame.Object.WingObject;
import cn.edu.dlut.WingGame.Object.WingObjectPool;

public class Test7 extends WingActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		WingGame.initWingGame(this);
		WingGame.setFPS(30, false);
		WingGame.setShowFPS(true);
		GLDevice.setSkewing(0, 0);

		WingScene main = new WingScene("main");
		WingScene menu = new WingScene("menu");
		WingScene about = new WingScene("about");
		WingGame.addScene(main);
		WingGame.addScene(menu);
		WingGame.addScene(about);
		WingGame.setCurrentScene("menu");

		WingLayer main_bg = new WingLayer("main_bg");
		menu.addLayer(main_bg);
		WingImage mainbg = new WingImage("gamedemo/main.jpg");
		mainbg.setPos(0, 0);
		main_bg.addObject(mainbg);

		WingGUI gui = new WingGUI("gui");
		menu.addLayer(gui);
		WingBitmapButton bt_start = new WingBitmapButton("bt_start");
		bt_start.set(50, 300, 230, 50);
		bt_start.setBitmap("gamedemo/newgame1.png", "gamedemo/newgame2.png");
		gui.addObject(bt_start);
		WingBitmapButton bt_about = new WingBitmapButton("bt_about");
		bt_about.set(50, 350, 230, 50);
		bt_about.setBitmap("gamedemo/about1.png", "gamedemo/about2.png");
		gui.addObject(bt_about);
		WingBitmapButton bt_exit = new WingBitmapButton("bt_exit");
		bt_exit.set(50, 400, 230, 50);
		bt_exit.setBitmap("gamedemo/exit1.png", "gamedemo/exit2.png");
		gui.addObject(bt_exit);

		WingLayer game_bg = new WingLayer("game_bg");
		main.addLayer(game_bg);
		ShotPool game_shot = new ShotPool("game_shot");
		main.addLayer(game_shot);
		WingLayer game_player = new WingLayer("game_player");
		main.addLayer(game_player);
		EnemyPool game_enemy = new EnemyPool("game_enemy");
		game_enemy.setMax(10);
		main.addLayer(game_enemy);
		WingLayer game_over = new WingLayer("game_over");
		main.addLayer(game_over);

		BG bg = new BG();
		game_bg.addObject(bg);

		Player player = new Player();
		game_player.addObject(player);

		GameOver game_overtext = new GameOver();
		game_over.addObject(game_overtext);

		WingLayer textlayer = new WingLayer();
		about.addLayer(textlayer);
		textlayer.addObject(bg);
		WingFont about_text = new WingFont(28, new WingColor(0, 255, 66));
		about_text.setTextRect(new Rect(30, 180, 400, 480));
		about_text.setTextStr("WingGame 游戏引擎");
		textlayer.addObject(about_text);
		WingGUI about_gui = new WingGUI("about_gui");
		about.addLayer(about_gui);
		WingBitmapButton bt_back = new WingBitmapButton("bt_back");
		bt_back.set(80, 400, 230, 50);
		bt_back.setBitmap("gamedemo/ok1.png", "gamedemo/ok2.png");
		about_gui.addObject(bt_back);

		EnemyManger enemymanger = new EnemyManger();
		main.addLayer(enemymanger);

		GUIManger guimanger = new GUIManger();
		menu.addLayer(guimanger);
		about.addLayer(guimanger);
	}

}

class Player extends WingImage {
	private int count = 0;

	public Player() {
		super("gamedemo/WingGame.png");
		setName("myPlayer");
		setPos(150, 350);
		setRota_Alpha(0, 1f);
		setSize(100, 100);
	}

	@Override
	public void update() {
		if (!WingTouch.isTouchDown)
			return;
		if ((WingTouch.X > x) && (WingTouch.X < x + width) && (WingTouch.Y > y)
				&& (WingTouch.Y < y + height)) {
			x = WingTouch.X - width / 2;
			y = WingTouch.Y - height / 2;
			if (count < 5)
				count++;
			else {
				count = 0;
				WingScene scene;
				ShotPool pool;
				Shot tmp;
				scene = (WingScene) WingGame.getScene("main");
				pool = (ShotPool) scene.getLayer("game_shot");
				tmp = (Shot) pool.getObject();
				tmp.setPos(x + 35, y + 10);
				tmp.setState(ObjectState.normal);
				tmp = (Shot) pool.getObject();
				tmp.setPos(x + 45, y + 10);
				tmp.setState(ObjectState.normal);
			}
		}

	}

	public boolean check(float sx, float sy) {
		if ((sx - x - 50) * (sx - x - 50) + (sy - y - 50) * (sy - y - 50) < 65 * 65)
			return true;
		else
			return false;
	}
}

class Enemy extends WingImage {
	private float v_x = 0;
	private float v_y = 4;

	public Enemy() {
		super("gamedemo/enemy.png");
		setSize(64, 64);
	}

	@Override
	public void update() {
		WingScene scene;
		WingLayer layer;
		Player player;
		scene = (WingScene) WingGame.getScene("main");
		layer = (WingLayer) scene.getLayer("game_player");
		player = (Player) layer.getObject("myPlayer");

		if (player.check(x + 32, y + 32)) {
			layer = (WingLayer) scene.getLayer("game_over");
			layer.getObject("GameOver").setState(ObjectState.normal);
		}
		if (Math.random() * 100 > 90)
			v_x = (float) (Math.random() * 4.0 - 2.0);
		y += v_y;
		x += v_x;
		if (y > 640 || x < -64 || x > 480)
			setState(ObjectState.died);
	}

	public boolean check(float sx, float sy) {
		if ((sx - x - 32) * (sx - x - 32) + (sy - y - 32) * (sy - y - 32) < 30 * 30)
			return true;
		else
			return false;
	}
}

class Shot extends WingImage {
	public Shot() {
		super("gamedemo/shot.png");
		setSize(16, 16);
	}

	@Override
	public void update() {
		WingScene scene;
		EnemyPool pool;
		scene = (WingScene) WingGame.getScene("main");
		pool = (EnemyPool) scene.getLayer("game_enemy");
		Enemy tmp;
		for (int i = 0; i < pool.getSize(); i++) {
			tmp = (Enemy) pool.getObject(i);
			if (tmp.check(x, y)) {
				tmp.setState(ObjectState.died);
				setState(ObjectState.died);
				break;
			}
		}

		y -= 4;
		if (y < 0 || y > 640 || x < 0 || x > 480)
			setState(ObjectState.died);
	}
}

class BG extends WingObject {
	private GLTexture tex = new GLTexture();
	private float y = 0;

	public BG() {
		tex.creatTexture("gamedemo/bg.jpg");
	}

	@Override
	public void render() {
		tex.drawEx(0, y, 320, 480 - y, 0, 0, 320, 480 - y, 0, 1);
		tex.drawEx(0, 0, 320, y, 0, 480 - y, 320, y, 0, 1);
	}

	@Override
	public void update() {
		if (y >= 480)
			y = 0;
		else
			y++;
	}
}

class GameOver extends WingFont {
	private int count = 0;

	public GameOver() {
		super(38, new WingColor(0, 255, 66));
		setName("GameOver");
		setTextRect(new Rect(50, 150, 400, 480));
		setTextStr("GAME OVER");
		setState(ObjectState.stop);
	}

	@Override
	public void update() {
		if (count < 30)
			count++;
		else {
			setState(ObjectState.stop);
			WingScene scene;
			WingGUI gui;
			WingObjectPool pool;
			scene = (WingScene) WingGame.getScene("menu");
			gui = (WingGUI) scene.getLayer("gui");
			gui.setActive(true);
			scene = (WingScene) WingGame.getScene("main");
			pool = (EnemyPool) scene.getLayer("game_enemy");
			pool.clear();
			pool = (ShotPool) scene.getLayer("game_shot");
			pool.clear();
			WingGame.setCurrentScene("menu");
			count = 0;
		}
	}
}

class GUIManger extends WingObject {
	public GUIManger() {
		setState(ObjectState.update);
	}

	@Override
	public void render() {
	}

	@Override
	public void update() {
		WingScene scene;
		WingGUI gui;
		WingBitmapButton btn;
		scene = (WingScene) WingGame.getScene("menu");
		gui = (WingGUI) scene.getLayer("gui");
		btn = (WingBitmapButton) gui.getObject("bt_start");
		if (btn.getButtonState()) {
			gui.setActive(false);
			WingGame.setCurrentScene("main");
			return;
		}
		btn = (WingBitmapButton) gui.getObject("bt_about");
		if (btn.getButtonState()) {
			gui.setActive(false);
			scene = (WingScene) WingGame.getScene("about");
			gui = (WingGUI) scene.getLayer("about_gui");
			gui.setActive(true);
			WingGame.setCurrentScene("about");
			return;
		}
		btn = (WingBitmapButton) gui.getObject("bt_exit");
		if (btn.getButtonState()) {
			WingGame.quitWingGame();
			return;
		}
		scene = (WingScene) WingGame.getScene("about");
		gui = (WingGUI) scene.getLayer("about_gui");
		btn = (WingBitmapButton) gui.getObject("bt_back");
		if (btn.getButtonState()) {
			gui.setActive(false);
			scene = (WingScene) WingGame.getScene("menu");
			gui = (WingGUI) scene.getLayer("gui");
			gui.setActive(true);
			WingGame.setCurrentScene("menu");
			return;
		}
	}
}

class EnemyManger extends WingObject {
	private int count = 0;

	public EnemyManger() {
		setState(ObjectState.update);
	}

	@Override
	public void render() {
	}

	@Override
	public void update() {
		if (count < 30) {
			count++;
		} else {
			count = 0;
			WingScene scene;
			EnemyPool pool;
			scene = (WingScene) WingGame.getScene("main");
			pool = (EnemyPool) scene.getLayer("game_enemy");
			Enemy tmp = (Enemy) pool.getObject();
			tmp.setPos((float) (Math.random() * 300 + 10 - 32), -60f);
			tmp.setState(ObjectState.normal);
		}
	}

}

class EnemyPool extends WingObjectPool {
	public EnemyPool(String str) {
		super(str);
	}

	@Override
	public WingObject newObject() {
		return new Enemy();
	}
}

class ShotPool extends WingObjectPool {
	public ShotPool(String str) {
		super(str);
	}

	@Override
	public WingObject newObject() {
		return new Shot();
	}
}
